<template>
    <el-card>
        <el-table border style="margin:10px 0" :data="skuArr">
            <el-table-column label="序号" type="index" width="80px" align="center"></el-table-column>
            <el-table-column label="名称" show-overflow-tooltip width="240px" prop="skuName"></el-table-column>
            <el-table-column label="描述" show-overflow-tooltip width="240px" prop="skuDesc"></el-table-column>
            <el-table-column label="默认图片" width="240px">
                <template #="{ row, $index }">
                    <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px">
                </template>
            </el-table-column>
            <el-table-column label="重量" show-overflow-tooltip width="240px" prop="weight"></el-table-column>
            <el-table-column label="价格" show-overflow-tooltip width="240px" prop="price"></el-table-column>
            <el-table-column label="操作" fixed="right" show-overflow-tooltip width="300px">
                <template #="{ row, $index }">
                    <el-button type="primary" size="small" :icon="row.isSale == 0 ? 'Top' : 'Bottom'"
                        @click="updateSale(row)"></el-button>
                    <el-button type="primary" size="small" icon="Edit" @click="updateSku"></el-button>
                    <el-button type="primary" size="small" icon="InfoFilled" @click="findSku(row)"></el-button>
                    <el-popconfirm :title="`你确定要删除${row.skuName}吗`" width="200px" @confirm="remove(row)">
                        <template #reference>
                            <el-button type="primary" size="small" icon="Delete"></el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 40]"
            :background="true" layout="prev, pager, next, jumper,->,sizes,total" :total="total" @size-change="handler"
            @current-change="getHasSku" />

        <!-- 抽屉组件,展示商品详情 -->
        <el-drawer v-model="drawer">
            <template #header>
                <h4>查看商品详情</h4>
            </template>
            <template #default>
                <el-row style="margin:10px 0">
                    <el-col :span="6">华为</el-col>
                    <el-col :span="18">{{ skuInfo.skuName }}</el-col>
                </el-row>
                <el-row style="margin:10px 0">
                    <el-col :span="6">描述</el-col>
                    <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
                </el-row>
                <el-row style="margin:10px 0">
                    <el-col :span="6">价格</el-col>
                    <el-col :span="18">{{ skuInfo.price }}</el-col>
                </el-row>
                <el-row style="margin:10px 0">
                    <el-col :span="6">平台属性</el-col>
                    <el-col :span="18">
                        <el-tag style="margin:0 5px" v-for="item in skuInfo.skuAttrValueList" :key="item.id"> {{
                            item.valueName }}</el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin:10px 0">
                    <el-col :span="6">销售属性</el-col>
                    <el-col :span="18">
                        <el-tag style="margin:0 5px" v-for="item in skuInfo.skuSaleAttrValueList" :key="item.id"> {{
                            item.saleAttrValueName
                        }}</el-tag>
                    </el-col>
                </el-row>
                <el-row style="margin:10px 0">
                    <el-col :span="6">商品图片</el-col>
                    <el-col :span="18">
                        <el-carousel :interval="4000" type="card" height="200px">
                            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                                <img :src="item.imgUrl" alt="" style="width: 100%; height: 100%;">
                            </el-carousel-item>
                        </el-carousel>
                    </el-col>
                </el-row>

            </template>
        </el-drawer>
    </el-card>
</template>
<script setup lang='ts'>
import { ref, onMounted, watch } from 'vue'
//引入SKU相关类型
import type { SkuResponseData, SkuData, SkuInfoData } from '@/api/product/sku/type';
//导入获取数据的函数
import { reqSkuList, reqSale, reqCancelSale, reqSkuInfo, reqDelete } from '@/api/product/sku/index'
import { ElMessage } from 'element-plus';
//设置当前页
let pageNo = ref<number>(1)
//设置当前单页数目
let pageSize = ref<number>(10)
//设置总数据数目
let total = ref<number>(50)
//存储sku数据
let skuArr = ref<SkuData[]>([])
//控制抽屉的显示与隐藏
let drawer = ref<boolean>(false);
//定义一个用于存储SKU信息的数组
let skuInfo = ref<SkuData>({})
//组件挂载完成时获取数据
onMounted(() => {
    getHasSku()
})
//定义获取数据的函数
const getHasSku = async (pager = 1) => {
    //当前分页器页码
    pageNo.value = pager;
    let result: SkuResponseData = await reqSkuList(pageSize.value, pageNo.value);
    if (result.code == 200) {
        total.value = result.data.total;
        skuArr.value = result.data.records;
    } else {

    }
}
//分页器下拉菜单变化的回调
const handler = () => {
    getHasSku()
}
//更新商品上下架状态的按钮回调
const updateSale = async (row: SkuData) => {
    // 判断isSale的值,是1则是上架,发送下架,是0则是下架,发送上架
    if (row.isSale == 1) {
        let result = await reqCancelSale((row.id as number))
        if (result.code == 200) {
            ElMessage({
                type: 'success',
                message: '下架成功',
            })
            // 请求成功则更新数据
            getHasSku(pageNo.value)
        } else {
            ElMessage({
                type: 'error',
                message: '下架失败',
            })
        }
    } else {
        let result = await reqSale((row.id as number))
        if (result.code == 200) {
            console.log(result)
            ElMessage({
                type: 'success',
                message: '上架成功',
            })
            // 请求成功则更新数据
            getHasSku(pageNo.value)
        } else {
            ElMessage({
                type: 'error',
                message: '上架失败',
            })
        }
    }
}
//修改Sku的回调
const updateSku = () => {
    ElMessage({
        type: "success",
        message: '正在研发中'
    })
}
//展示商品详情的回调
const findSku = async (row: SkuData) => {
    let result: SkuInfoData = await reqSkuInfo((row.id as number));
    skuInfo.value = result.data;
    drawer.value = true
}
//删除按钮的回调
const remove = async (row: SkuData) => {
    let result = await reqDelete((row.id as number))
    if (result.code == 200) {
        ElMessage({
            type: 'success',
            message: "删除成功"
        })
        getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
    } else {
        ElMessage({
            type: 'error',
            message: "这个不行"
        })
    }
}
</script>
<style lang='scss' scoped>
.el-carousel__item h3 {
    color: #475669;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
    text-align: center;
}

.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
    background-color: #d3dce6;
}
</style>